<?php
	autoLoader();
	
	$strAction = ( true == isset( $_POST['operation'] ) && true == valStr( $_POST['operation'] ) ) ? $_POST['operation'] : NULL;
	
	switch( $strAction ) {
		case 'insert':
			handleInsertData();
			break;
	
		case 'update':
			handleUpdateData();
			break;
	
		case 'delete':
			handleDeleteData();
			break;
	
		case 'quick_insert':
			handleQuickInsertData();
			break;
	
		default:
			display( 'Provided action not found' );
			exit;
			break;
	}
	exit;
	
	function handleInsertData() {
		
		if( false == isset( $_POST['operation'] ) || false == valStr( $_POST['patient_id'] ) ) {
			$str = '';
			$str .= '<form name=myform method=post action=modules.php?file=lab_case_paper&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
			$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
			echo $str;
			exit;
		}
		
		$objPatient = new Patients();
		$objPatient = $objPatient->fetchPatientsById( $_POST['patient_id'] );
		
		if( false == valObj( $objPatient, 'BasePatients' ) ) {
			echo 'Patient not found.';
			exit;
		}
		
		$objChargeType = new ChargeTypes();
		$objChargeType = $objChargeType->fetchChargeTypesById( $_POST['charge_applied'] );
		
		if( false == valObj( $objChargeType, 'BaseChargeTypes' ) ) {
			echo 'Failed to load charge types.';
			exit;
		}
		
		$intChargeAmount = ( PatientType::PATIENT_CB == $objPatient->getPatientTypeId() ) ? $objChargeType->getCbArea() : $objChargeType->getOutOfCa();
	
		if( true == isset( $_POST['operation'] ) && 'insert' == strtolower( $_POST['operation'] ) ) {
			$objBlood 	= new BaseBloods();
			$objCharge 	= new BaseCharges();
			
			$boolIsValid = true;
			
			$objBlood->setId( $objBlood->fetchNextId() );
			$objBlood->setPatientId( $_POST['patient_id'] );
			$objBlood->setCasePaperId( $_POST['case_paper_id'] );
			$objBlood->setDoctorId( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objBlood->setChargeId( 'NULL' );
			$objBlood->setRemark( $_POST['remark'] );
			$objBlood->setDateOfExamination( date( 'Y-m-d H:M:s' ) );
			$objBlood->setHb( $_POST['hb'] );
			$objBlood->setTotalWbcs( $_POST['wbc'] );
			$objBlood->setDifferentialCount( $_POST['differential_count'] );
			$objBlood->setPolymorphs( $_POST['polymorphs'] );
			$objBlood->setEosinophils( $_POST['eosinophils'] );
			$objBlood->setBasophi( $_POST['basophi'] );
			$objBlood->setLymphocytes( $_POST['lymphocytes'] );
			$objBlood->setMonocytes( $_POST['monocytes'] );
			$objBlood->setMalarialParasite( $_POST['malarial_parasite'] );
			$objBlood->setEsrInListHour( $_POST['esr_in_hour'] );
			$objBlood->setSpecialFindings( $_POST['special_findings'] );
			$objBlood->setBt( $_POST['bt'] );
			$objBlood->setCt( $_POST['ct'] );
			$objBlood->setGramsMm( 'NULL' );
			$objBlood->setEximationCount( 0 );
			$objBlood->setCreatedOn( 'now()' );
			$objBlood->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objBlood->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objBlood->setUpdatedOn( 'now()' );
			
			if( 1 != $objBlood->insert() ) {
				$boolIsValid &= false;
			}
			
			$objCharge->setId( $objCharge->fetchNextId() );
			$objCharge->setChargeTypeId( $objChargeType->getId() );
			$objCharge->setChargeCatagoryId( $objChargeType->getChargeCatagoryId() );
			$objCharge->setMinAmount( $intChargeAmount );
			$objCharge->setMaxAmount( $intChargeAmount );
			$objCharge->setRate( $intChargeAmount );
			$objCharge->setDepartmentId( DepartmentType::DEPARTMENT_LAB );
			$objCharge->setPatientId( $objPatient->getId() );
			$objCharge->setReferanceId( $objBlood->getId() );
			$objCharge->setDeletedBy( 0 );
			$objCharge->setDeletedOn( 'now()' );
			$objCharge->setCreatedOn( 'now()' );
			$objCharge->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objCharge->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objCharge->setUpdatedOn( 'now()' );
			
			if( 1 != $objCharge->insert() ) {
				$boolIsValid &= false;
			}
			
			$objBlood->setChargeId( $objCharge->getId() );
			
			if( 1 != $objBlood->update() ) {
				$boolIsValid &= false;
			}
			
			
			if( true == $boolIsValid ) {
				echo '1';
				exit;
			} else {
				echo '0';
				exit;
			}
		}
		return true;
	}
	
	function handleUpdateData() {
		
		if( false == isset( $_POST['operation'] ) || false == valStr( $_POST['patient_id'] ) ) {
			$str = '';
			$str .= '<form name=myform method=post action=modules.php?file=lab_case_papers&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
			$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
			echo $str;
			exit;
		}
		
		$objPatient = new Patients();
		$objPatient = $objPatient->fetchPatientsById( $_POST['patient_id'] );
		
		if( false == valObj( $objPatient, 'BasePatients' ) ) {
			echo 'Patient not found.';
			exit;
		}
		
		$objChargeType = new ChargeTypes();
		$objChargeType = $objChargeType->fetchChargeTypesById( $_POST['charge_applied'] );
		
		if( false == valObj( $objChargeType, 'BaseChargeTypes' ) ) {
			echo 'Failed to load charge types.';
			exit;
		}
		
		$intChargeAmount = ( PatientType::PATIENT_CB == $objPatient->getPatientTypeId() ) ? $objChargeType->getCbArea() : $objChargeType->getOutOfCa();
	
		if( true == isset( $_POST['operation'] ) && 'update' == strtolower( $_POST['operation'] ) ) {
			
			$boolIsValid = true;
			
			$objBlood = new Bloods();
			$objBlood = $objBlood->fetchBloodsById( $_POST['blood_id'] );
			
			if( false == valObj( $objBlood, 'BaseBloods' ) ) {
				echo 'Failed to load Blood Report.';
				exit;
			}
			
			$objCharge = new Charges();
			$objCharge = $objCharge->fetchChargesById( $_POST['charge_id'] );
			
			if( false == valObj( $objBlood, 'BaseBloods' ) || false == valObj( $objCharge, 'BaseCharges') ) {
				echo 'Falied to load Blood and Charge.';
				exit;
			}
			
// 			$objBlood->setPatientId( $objBlood->getPatientId() );
// 			$objBlood->setCasePaperId( $objBlood->getCasePaperId() );
			$objBlood->setDoctorId( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objBlood->setChargeId( $objCharge->getId() );
			$objBlood->setRemark( $_POST['remark'] );
			$objBlood->setDateOfExamination( date( 'Y-m-d H:M:s' ) );
			$objBlood->setHb( $_POST['hb'] );
			$objBlood->setTotalWbcs( $_POST['wbc'] );
			$objBlood->setDifferentialCount( $_POST['differential_count'] );
			$objBlood->setPolymorphs( $_POST['polymorphs'] );
			$objBlood->setEosinophils( $_POST['eosinophils'] );
			$objBlood->setBasophi( $_POST['basophi'] );
			$objBlood->setLymphocytes( $_POST['lymphocytes'] );
			$objBlood->setMonocytes( $_POST['monocytes'] );
			$objBlood->setMalarialParasite( $_POST['malarial_parasite'] );
			$objBlood->setEsrInListHour( $_POST['esr_in_hour'] );
			$objBlood->setSpecialFindings( $_POST['special_findings'] );
			$objBlood->setBt( $_POST['bt'] );
			$objBlood->setCt( $_POST['ct'] );
			$objBlood->setGramsMm( 'NULL' );
			$objBlood->setEximationCount( 0 );
// 			$objBlood->setCreatedBy( $objBlood->getCreatedBy() );
			$objBlood->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objBlood->setUpdatedOn( 'now()' );
			
			if( 1 != $objBlood->update() ) {
				$boolIsValid &= false;
			}
			
			$objCharge->setChargeTypeId( $objChargeType->getId() );
			$objCharge->setChargeCatagoryId( $objChargeType->getChargeCatagoryId() );
			$objCharge->setMinAmount( $intChargeAmount );
			$objCharge->setMaxAmount( $intChargeAmount );
			$objCharge->setRate( $intChargeAmount );
			$objCharge->setDepartmentId( DepartmentType::DEPARTMENT_LAB );
			$objCharge->setPatientId( $objPatient->getId() );
			$objCharge->setReferanceId( $objBlood->getId() );
			$objCharge->setDeletedBy( 0 );
			$objCharge->setDeletedOn( 'now()' );
			$objCharge->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
			$objCharge->setUpdatedOn( 'now()' );
				
			if( 1 != $objCharge->update() ) {
				$boolIsValid &= false;
			}
			
			if( true == $boolIsValid ) {
				echo '1';
				exit;
			} else {
				echo '0';
				exit;
			}
		}
		return true;
	}
	
	function handleDeleteData() {
	
	}
	
	function handleQuickInsertData() {
	
		if( false == isset( $_GET['case_paper_id'] ) || false == isset( $_GET['charge_amount'] ) ) {
			echo 'Data sending failed.';
			exit;
		}
	
		$objCasePaper = new CasePapers();
		$objCasePaper = $objCasePaper->fetchCasePapersById( $_GET['case_paper_id'] );
	
		if( true == valObj( $objCasePaper, 'BaseCasePapers' ) ) {
			$objCasePaper->setAmountPaid( $_GET['charge_amount'] );
			$objCasePaper->setDiagnosisId( ( ( true == valStr( $objCasePaper->getDiagnosisId() ) ) ? $objCasePaper->getDiagnosisId() : NULL ) );
			$objCasePaper->setChargeTypeId( ( ( true == valStr( $objCasePaper->getChargeTypeId() ) ) ? $objCasePaper->getChargeTypeId() : NULL ) );
	
			if( 1 == $objCasePaper->update() ) {
				header( 'Location:modules.php?file=print_case_paper&type=print&case_paper_id=' . $_GET['case_paper_id'] . '&patient_id=' . $_GET['patient_id'] . '&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] );
				exit;
			} else {
				echo 'Case Paper upadte successfully.';
			}
		} else {
			echo 'Failed to load case paper.';
		}
	}
?>